<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Colony' );
?>
					<div class="blok">
<?php
print_operations_tab( '' );
?>
					</div>
					<br />
<?php
$gameturn = getCurrentTurn();
printGlobalSoldiers( $_SESSION[ 'ruler_id' ] );
include( "../global/resources.php" );

if( !isset( $_GET[ 'id' ] ) || !is_numeric( $_GET[ 'id' ] ) ) {
	$id = -1;	
}
else {
	$id = formatInput( $_GET[ 'id' ] );
}

$query = "SELECT colony.population, colony.type, colony.colony_level_id, colony.name, colony.transport_turn, colony.colony_id, colony_level.type_name, colony_level.gold, colony_level.iron, colony_level.coal" .
		", colony_level.recruits, colony_level.infantry, colony_level.cavalry, colony_level.artillery, colony_level.travel_time , colony_level.growth " .
		"FROM colony " .
		"JOIN colony_level ON colony.colony_level_id = colony_level.colony_level_id " .
		"WHERE colony_id = '" . $id . "' AND ruler_id = " . $_SESSION[ 'ruler_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC);

if( isset( $row[ 'name' ] ) ) {
	print( "<br />\r\n" );
	$query2 = "SELECT * FROM colony WHERE ruler_id = " . $_SESSION['ruler_id'] . " ORDER BY name";
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$num = mysql_numrows( $result2 );
	$i = 0;
	while ( $i < $num ) {
		if( mysql_result( $result2, $i, "colony_id" ) == $id ) {
			if( $i < $num - 1 ) {
				print( "<div id=\"text_right\"><a href=\"colony.php?id=" . mysql_result( $result2, ( $i + 1 ), "colony_id" ) . "\">" . translate( 'Next' ) . "</a></div>\r\n" );
			}
			if( $i > 0 ) {
				print( "<div id=\"text_left\"><a href=\"colony.php?id=" . mysql_result( $result2, ( $i - 1 ), "colony_id" ) . "\">" . translate( 'Previous' ) . "</a></div>\r\n" );
			}
		}
		$i ++;
	}
	
	print "<br /><table id= \"twothird\"><th colspan=\"2\">" . translate( $row[ 'type_name' ] ) . ": " . $row[ 'name' ] . "</th>\r\n";
	print( "<tr id=\"even\"><td>" . translate( 'Population' ) . "</td><td align=\"right\">" . number_format( $row[ 'population' ] ) . "</td></tr>\r\n" );
	
	$query2 = "SELECT population " .
			"FROM colony_level WHERE colony_level_id = " . ( $row[ 'colony_level_id' ] + 1 );
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC);
	if( isset( $row2[ 'population' ] ) ) {
		$countdown = ( int ) ceil( ( $row2[ 'population' ] - $row[ 'population' ] ) / $row[ 'growth' ] );
		//print( $row2[ 'population' ] . "<br />" );
		//print( $row[ 'population' ] . "<br />" );
		//print( $row[ 'growth' ] . "<br />" );
		if( $countdown == 1 ) {
			$countdown = translate( '1 day' );
		}
		else {
			$countdown = $countdown . " " . translate( 'days' );
		}
		print( "<tr id=\"even\"><td>" . translate( 'Estimated time until production increase' ) . "</td><td align=\"right\">$countdown</td></tr>\r\n" );
	}
	if( $row[ 'transport_turn' ] - $gameturn == 1 ) {
		print( "<tr id=\"even\"><td>" . translate( 'Next transport' ) . "</td><td align=\"right\">" . translate( '1 day' ) . "</td></tr>\r\n" );
	}
	else { 
		print( "<tr id=\"even\"><td>" . translate( 'Next transport' ) . "</td><td align=\"right\">" . number_format( $row[ 'transport_turn' ] - $gameturn ) . " " . translate( 'days' ) . "</td></tr>\r\n" );
	}
	print( "<tr id=\"even\"><td>" . translate( 'Transport travel time' ) . "</td><td align=\"right\">" . number_format( $row[ 'travel_time' ] ) . " " . translate( 'days' ) . "</td></tr>\r\n" );
	if( $row[ 'type' ] == 0 ) {
		$gold = $row[ 'gold' ];
		$coal = round( 0.1 * $row[ 'coal' ] );
		$iron = round( 0.1 * $row[ 'iron' ] );
		print( "<tr id=\"even\"><td>" . translate( 'Main resource' ) . "</td><td align=\"right\"><strong class=\"gold\">" . translate( 'Gold' ) . "</strong></td></tr>\r\n" );
		print( "<tr id=\"even\"><td>" . translate( 'Gold per transport' ) . "</td><td align=\"right\"><strong class=\"gold\">" . number_format( $gold ) . "</strong></td></tr>\r\n" );
	}
	else if( $row[ 'type' ] == 1 ) {
		$gold = round( 0.1 * $row[ 'gold' ] );
		$coal = $row[ 'coal' ];
		$iron = round( 0.1 * $row[ 'iron' ] );
		print( "<tr id=\"even\"><td>" . translate( 'Main resource' ) . "</td><td align=\"right\"><strong class=\"coal\">" . translate( 'Coal' ) . "</strong></td></tr>\r\n" );
		print( "<tr id=\"even\"><td>" . translate( 'Coal per transport' ) . "</td><td align=\"right\"><strong class=\"coal\">" . number_format( $coal ) . "</strong></td></tr>\r\n" );
	}
	else {
		$gold = round( 0.1 * $row[ 'gold' ] );
		$coal = round( 0.1 * $row[ 'coal' ] );
		$iron = $row[ 'iron' ];
		print( "<tr id=\"even\"><td>" . translate( 'Main resource' ) . "</td><td align=\"right\"><strong id=\"iron\">" . translate( 'Iron' ) . "</strong></td></tr>\r\n" );
		print( "<tr id=\"even\"><td>" . translate( 'Iron per transport' ) . "</td><td align=\"right\"><strong id=\"iron\">" . number_format( $iron ) . "</strong></td></tr>\r\n" );
	}
	if( $row[ 'recruits' ] > 0 ) {
		print( "<tr id=\"even\"><td>" . translate( 'Recruits per transport' ) . "</td><td align=\"right\">" . number_format( $row[ 'recruits' ] ) . "</td></tr>\r\n" );
	}
	if( $row[ 'infantry' ] > 0 ) {
		print( "<tr id=\"even\"><td>" . translate( 'Infantry per transport' ) . "</td><td align=\"right\">" . number_format( $row[ 'infantry' ] ) . "</td></tr>\r\n" );
	}
	if( $row[ 'cavalry' ] > 0 ) {
		print( "<tr id=\"even\"><td>" . translate( 'Cavalry per transport' ) . "</td><td align=\"right\">" . number_format( $row[ 'cavalry' ] ) . "</td></tr>\r\n" );
	}
	if( $row[ 'artillery' ] > 0 ) {
		print( "<tr id=\"even\"><td>" . translate( 'Artillery per transport' ) . "</td><td align=\"right\">" . number_format( $row[ 'artillery' ] ) . "</td></tr>\r\n" );
	}
	print "</table><br />\r\n";
	
	$query = "SELECT * FROM colony_transport " .
			"WHERE colony_id = " . $row[ 'colony_id' ] .
			" ORDER BY turn";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$num = mysql_numrows( $result );
	
	$i = 0;
	if( $num > 0 ) {
		print "<br /><table id= \"twothird\"><th colspan=\"4\">" . translate( 'Resource transports' ) . "</th>\r\n";
		print( "<tr><td>" . translate( 'Arrival' ) . "</td><td>" . translate( 'Gold' ) . "</td><td>" . translate( 'Iron' ) . "</td><td>" . translate( 'Coal' ) . "</td></tr>" );
		while ( $num > $i ) {
		    if( $i % 2 == 0 ) {
		        print "<tr id=\"even\">";
		    }
		    else {
		        print "<tr>";
		    }
			if( mysql_result( $result, $i, "turn" ) - $gameturn == 1 ) {
				print( "<td align=\"right\">" . translate( '1 day' ) . "</td>" );
			}
			else {
				print( "<td align=\"right\">" . number_format( mysql_result( $result, $i, "turn" ) - $gameturn ) . " " . translate( 'days' ) . "</td>" );
			}
			print( "<td align=\"right\">" .
					"<strong class=\"gold\">" . number_format( mysql_result( $result, $i, "gold" ) ) . "</strong></td>" .
					"<td align=\"right\"><strong class=\"iron\">" . number_format( mysql_result( $result, $i, "iron" ) ) . "</strong></td>" .
					"<td align=\"right\"><strong class=\"coal\">" . number_format( mysql_result( $result, $i, "coal" ) ) . "</strong>" .
					"</td>" );
			print( "</tr>" );
			$i ++;
		}
	}
	else {
		print "<div class=\"blok\">\r\n";
		print translate( 'No transports underway.' ) . "<br/>\r\n";
		print "</div><br/>\r\n";
	}
}
else {
	print "<div class=\"blok\">\r\n";
	print translate( 'No proper colony id used.' ) . "<br/>\r\n";
	print "</div><br/>\r\n";
}
aos_footer();
?>